package com.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.code.constant.BannerConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.mapper.BannerMapper;
import com.code.domain.Banner;
import com.code.service.BannerService;

@Service
@CacheConfig(cacheNames = "com.code.service.impl.BannerServiceImpl")
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService{

    @Autowired
    private BannerMapper bannerMapper;
    /**
     * 查询轮播图的接口
     * @return
     */
    @Override
    @Cacheable(key = BannerConstant.BANNER_CACHE_PREFIX)
    public List<String> getBanner() {
        List<Object> objects = bannerMapper.selectObjs(new LambdaQueryWrapper<Banner>()
                .select(Banner::getPath)
                .orderByDesc(Banner::getBeginTime)
                .last(" limit 5 ")
        );
        List<String> paths = objects.stream().map(Object::toString).collect(Collectors.toList());
        return paths;
    }
}
